/**
 * 洋葱模型
 */
const Koa = require('koa')
const app = new Koa()

const indent = (n) => new Array(n).join('&nbsp;')

// 中间件  use中的函数就是中间件
const mid1 = async (ctx, next) => {
  ctx.body = '<h3>请求: 第一层中间件</h3>'
  await next()
  ctx.body+= `<h3>响应：第一层中间件</h3>`
} 

const mid2 = async (ctx, next) => {
  ctx.body += `<h3>${indent(4)}请求：第二层中间件</h3>`
  await next()
  ctx.body+= `<h3>${indent(4)}响应：第二层中间件</h3>`
}

const mid3 = async (ctx, next) => {
  ctx.body += `<h3>${indent(8)}请求：第三层中间件</h3>`
  await next()
  ctx.body+= `<h3>${indent(8)}响应：第三层中间件</h3>`
}

app.use(mid1)
app.use(mid2)
app.use(mid3)

app.use(async (ctx, next) => {
  ctx.body += `<h1 style="color:red; font-size: 20px;">${indent(12)}koa 核心业务</h1>`
})

app.listen(8888, () => console.log(`server port: ${8888}`))


